# -*- autoconf -*-
#########################################
##
# Checks for libraries that are run
# before the agent module tests.
##
#########################################


##
#   RPM checks
#
##
AC_CHECK_HEADERS(rpm/rpmlib.h rpm/header.h)

AC_CHECK_HEADER(rpm/rpmlib.h,rpmlib_h=yes,rpmlib_h=no)
AC_CHECK_HEADER(rpm/header.h,rpmheader_h=yes,rpmheader_h=no)

if test "x$rpmheader_h" = "xno" -o "x$rpmlib_h" = "xno" ; then
  if test "x$with_rpm" = "xyes" ; then
    AC_MSG_ERROR([Could not find either the RPM header files needed and was specifically asked to use RPM support])
  else
    with_rpm=no
  fi
fi

#
# rpm libraries only needed for the host resources mib software
# installed tables (on linux in particular)
#
if test "x$with_rpm" != "xno"; then
  # ARG.  RPM is a real pain.
  # FWIW librpm.la, librpmio.la, and libpopt.la have correct dependencies.
  _rpmlibs=""

  # rpm-4.0.x needs rpmio.
  NETSNMP_SEARCH_LIBS(rpmGetPath, rpmio,,, $LMIBLIBS, _rpmlibs)
  
  # now check for rpm using the appropriate libraries.
  NETSNMP_SEARCH_LIBS(rpmReadConfigFiles, rpm, [
    AC_DEFINE(HAVE_LIBRPM, 1,
      [Define to 1 if you have the `rpm' library (-lrpm).])
    LMIBLIBS="$_rpmlibs $LMIBLIBS"
  ],, $LMIBLIBS, _rpmlibs)

  # rpm 4.6 has incompatible API, turn on the legacy one
  NETSNMP_SEARCH_LIBS(headerGet,, [
    AC_DEFINE(HAVE_HEADERGET, 1,
      [Define to 1 if you have the headerGet function.])],, $LMIBLIBS)
fi

#
# getfsstat/getvfsstat
#
AC_SEARCH_LIBS(getfsstat, [nbutil])
AC_CHECK_FUNCS(getfsstat)
AC_CHECK_FUNCS(getvfsstat)
AC_FUNC_GETMNTENT

#
# kinfo_get_cpus
#
AC_SEARCH_LIBS(kinfo_get_cpus, [kinfo])
AC_CHECK_FUNCS(kinfo_get_cpus)

#
# diskIOLAx requires exp which may require -lm
#
NETSNMP_SEARCH_LIBS([exp], [m],,,, [LMIBLIBS])

#
#   libkvm
#
NETSNMP_SEARCH_LIBS(kvm_read, kvm,,,, LAGENTLIBS)
NETSNMP_SEARCH_LIBS(kvm_openfiles, kvm,,,, LAGENTLIBS)
NETSNMP_SEARCH_LIBS(kvm_getprocs, kvm,,,, LMIBLIBS)
NETSNMP_SEARCH_LIBS(kvm_getproc2, kvm,,,, LMIBLIBS)
NETSNMP_SEARCH_LIBS(kvm_getfiles, kvm,,,, LMIBLIBS)
NETSNMP_SEARCH_LIBS(kvm_getfile2, kvm,,,, LMIBLIBS)
NETSNMP_SEARCH_LIBS(kvm_getswapinfo, kvm,,,, LMIBLIBS)
netsnmp_save_LIBS="$LIBS"
LIBS="$LAGENTLIBS $LMIBLIBS $LIBS"
AC_CHECK_FUNCS([kvm_openfiles kvm_getprocs kvm_getproc2 kvm_getswapinfo kvm_getfiles kvm_getfile2])
LIBS="$netsnmp_save_LIBS"

#
#   dynamic module support
#
NETSNMP_SEARCH_LIBS([dlopen], [dl],,, [$LAGENTLIBS $LSNMPLIBS], [LMIBLIBS])
netsnmp_save_LIBS="$LIBS"
LIBS="$LMIBLIBS $LAGENTLIBS $LSNMPLIBS $LIBS"
AC_CHECK_FUNCS([dlopen])
LIBS="$netsnmp_save_LIBS"

#
#   regex in process table
#
if test "x$with_pcre2" != "xno"; then
  AC_CHECK_HEADER([pcre2.h], [
      AC_DEFINE([HAVE_PCRE2_H], [1], [Define to 1 if you have <pcre2.h>.])
      pcre2_h=yes
    ],
    [pcre2_h=no], [#define PCRE2_CODE_UNIT_WIDTH 8]
  )
fi
if test "x$pcre2header_h" = "xno" -o "x$pcre2_h" = "xno" ; then
  if test "x$with_pcre2" = "xyes" ; then
    AC_MSG_ERROR([Could not find the pcre2 header file needed and was specifically asked to use pcre2 support])
  else
    with_pcre2=no
  fi
fi

if test "x$with_pcre2" != "xno"; then
  NETSNMP_SEARCH_LIBS([pcre2_match_8], [pcre2-8], [
    LMIBLIBS="$LMIBLIBS -lpcre2-8"
    ],,, LAGENTLIBS)
  AC_SUBST(LAGENTLIBS)
  AC_SUBST(LMIBLIBS)
fi

if test "x$with_pcre2" != "xyes"; then

if test "x$with_pcre" != "xno"; then
  AC_CHECK_HEADER([pcre.h], [
      AC_DEFINE([HAVE_PCRE_H], [1], [Define to 1 if you have <pcre.h>.])
      pcre_h=yes
    ],
    [pcre_h=no]
  )
fi
if test "x$pcreheader_h" = "xno" -o "x$pcre_h" = "xno" ; then
  if test "x$with_pcre" = "xyes" ; then
    AC_MSG_ERROR([Could not find the pcre header file needed and was specifically asked to use pcre support])
  else
    with_pcre=no
  fi
fi

#
# pcre libraries only needed for the ucd process agent mib software
#
if test "x$with_pcre" != "xno"; then
  NETSNMP_SEARCH_LIBS([pcre_exec], [pcre], [
    LMIBLIBS="$LMIBLIBS -lpcre"
    ],,, LAGENTLIBS)
  AC_SUBST(LAGENTLIBS)
  AC_SUBST(LMIBLIBS)
fi
fi
